using System;
using System.Collections.Generic;
using TTXNA.src.Util;
using Microsoft.Xna.Framework;
using TTXNA.src.Engine.Controls;
using Microsoft.Xna.Framework.Input;
using TTXNA.src.Engine.Input;

namespace TTXNA.src.Engine.GUI.Windows
{
    public class Window
    {
        private GUIManager parent;
        protected RectangleF screenRectangle;
        protected Vector2 minimumSize;
        protected Dictionary<int, Control> windowControls;
        protected bool isHidden;
        private bool alwaysOnTop;

        public Window(GUIManager parent)
        {
            this.parent = parent;

            windowControls = new Dictionary<int, Control>();
            screenRectangle = new RectangleF();
            minimumSize = Vector2.One;
        }

        public RectangleF Area
        {
            get { return screenRectangle; }
        }

        public Vector2 MinimumSize
        {
            get { return minimumSize; }
            set { minimumSize = value; }
        }

        public virtual void ResizeByFactor(float widthScale, float heightScale) {

        }

        public virtual bool ResizeTo(float width, float height) 
        {
            float widthFactor = width / screenRectangle.Width;
            float heightFactor = height / screenRectangle.Height;

            if (width < MinimumSize.X) { widthFactor = 1; }
            if (height < MinimumSize.Y) { heightFactor = 1; }

            foreach (Control c in windowControls.Values)
            {
                c.ResizeBy(widthFactor, heightFactor);
            }

            screenRectangle.Width = width;
            screenRectangle.Height = height;

            return true;
        }

        public virtual void MoveBy(float right, float down)
        {
            screenRectangle.X += right;
            screenRectangle.Y += down;
        }

        protected void SetScreenRectangle(RectangleF rect)
        {
            this.screenRectangle = rect;
        }

        public bool Contains(Point point)
        {
            return screenRectangle.Contains(point);
        }

        public virtual void Render()
        {
            if (!isHidden)
            {
                foreach (Control c in windowControls.Values)
                {
                    c.Render(screenRectangle.Location);
                }
            }
        }

        public bool MouseHandler(MouseData mouse)
        {
            MouseData relative = mouse;

            Point cursor = mouse.CursorLocation;
            bool result = false;

            if (Contains(mouse.CursorLocation))
            {
                foreach (Control c in windowControls.Values)
                {
                    relative.CursorLocation = new Point(cursor.X - (int)screenRectangle.X, cursor.Y - (int)screenRectangle.Y);
                    result = c.MouseHandler(relative) || result;
                }
                return true;
            }
            else
            {
                foreach (Control c in windowControls.Values)
                {
                    relative.CursorLocation = new Point(cursor.X - (int)screenRectangle.X, cursor.Y - (int)screenRectangle.Y);
                    c.MouseHandler(relative);
                }
                return false;
            }
        }

        public virtual void Close()
        {
            foreach (Control c in windowControls.Values)
            {
                c.Dispose();
            }
            //windowControls.Clear();

            parent.CloseWindow(this);
        }

        public void Hide()
        {
            isHidden = true;
        }

        public void Show()
        {
            isHidden = false;
        }

        public virtual void Update()
        {

        }

        public bool AlwaysOnTop
        {
            get { return alwaysOnTop; }
            set { alwaysOnTop = value; }
        }
    }
}
